/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.iprange.info;

import java.net.InetAddress;

/**
 * IPV4 地址工具类
 *
 * @author liangdelin
 */
public class IpToIntTools {

    /**
     * 把192.168.1.1/24 转化为int数组范围
     *
     * @param ipAndMask
     * @return int[]
     */
    public static int[] getIPIntScope(String ipAndMask) {

        String[] ipArr = ipAndMask.split("/");
        int netMask = Integer.valueOf(ipArr[1].trim());
        int ipInt = ipToInt(ipArr[0]);
        if (netMask == 32) {
            return new int[]{ipInt, ipInt};
        } else {
            int netIP = ipInt & (0xFFFFFFFF << (32 - netMask));
            int hostScope = (0xFFFFFFFF >>> netMask);
            return new int[]{netIP, netIP + hostScope};
        }
    }

    /**
     * 把IP地址转化为int
     *
     * @param ipAddr
     * @return int
     */
    public static int ipToInt(String ipAddr) {
        try {
            return bytesToInt(ipToBytesByInet(ipAddr));
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    /**
     * 根据位运算把 byte[] -> int
     *
     * @param bytes
     * @return int
     */
    public static int bytesToInt(byte[] bytes) {
        int addr = bytes[3] & 0xFF;
        addr |= ((bytes[2] << 8) & 0xFF00);
        addr |= ((bytes[1] << 16) & 0xFF0000);
        addr |= ((bytes[0] << 24) & 0xFF000000);
        return addr;
    }

    /**
     * 把int->ip地址
     *
     * @param ipInt
     * @return String
     */
    public static String intToIp(int ipInt) {
        return new StringBuilder().append(((ipInt >> 24) & 0xff)).append('.')
                .append((ipInt >> 16) & 0xff).append('.').append(
                        (ipInt >> 8) & 0xff).append('.').append((ipInt & 0xff))
                .toString();
    }

    /**
     * 把IP地址转化为字节数组
     *
     * @param ipAddr
     * @return byte[]
     */
    public static byte[] ipToBytesByInet(String ipAddr) {
        try {
            return InetAddress.getByName(ipAddr).getAddress();
        } catch (Exception e) {
            throw new IllegalArgumentException(ipAddr + " is invalid IP");
        }
    }

    public static void main(String[] args) {
        int[] ips = IpToIntTools.getIPIntScope("100.1.1.1/26");
        System.out.println(IpToIntTools.intToIp(ips[0]));
        System.out.println(IpToIntTools.intToIp(ips[1]));

        ips = IpToIntTools.getIPIntScope("100.1.1.64/27");
        System.out.println(IpToIntTools.intToIp(ips[0]));
        System.out.println(IpToIntTools.intToIp(ips[1]));

        ips = IpToIntTools.getIPIntScope("100.1.1.96/30");
        System.out.println(IpToIntTools.intToIp(ips[0]));
        System.out.println(IpToIntTools.intToIp(ips[1]));

        ips = IpToIntTools.getIPIntScope("100.1.1.100/32");
        System.out.println(IpToIntTools.intToIp(ips[0]));
        System.out.println(IpToIntTools.intToIp(ips[1]));
        
        
         System.out.println(Integer.parseInt("0000000100000001", 2));
         toBinString(-100);
         toBinString(100);

    }
    
    public static String toBinString(int i){
        System.out.println(Integer.toBinaryString(i));
        StringBuffer sb = new StringBuffer();
        int len = Integer.toBinaryString(i).length();
        
        return null;
    }
}
